Programs =
Algorithms + Data Structures
by Niklaus Wirth, 1976
https://en.wikipedia.org/wiki/Algorithms_%2B_Data_Structures_%3D_Programs
程式 =
演算法 + 資料
by 尼克勞斯·維爾特, 1976
https://zh.wikipedia.org/zh-tw/%E5%B0%BC%E5%85%8B%E5%8A%B3%E6%96%AF%C2%B7%E7%BB%B4%E5%B0%94%E7%89%B9
Data Structure:
剛剛上一位講者: Ning 已經教過了,
別忘了要記得用swirl()複習喔!
目的是用來處理「特定資料」,
根據演算法將多個函式或是指令組成的集合
簡單說,寫一個程式,其實和 寫一段文章,或是一篇小說差不多,
數學上叫函數,
程式語言中叫函式
1. 可以把「相同目的」的"指令"和"資料" 整理在一起 2. 可重複使用 3. "可方便閱讀程式"
by 民明書房 Noah
?function(){}
函式名稱 = function(引數) {
程式的內容........
}
引數:表示可以把資料從function外面引導到function裏面的橋樑,
引數不一定要給
提醒:愛護生命,建議千萬不要用中文當作變數及函式名字,現在用中文只是為了說明
函式名稱()
再次提醒:家人朋友都很愛你,別想不開用中文當作變數及函式名字
請在R 中顯示:Hello World, R
"Hello World, R"
print("Hello World, R")
paste("Hello World, R")
print("Hello World, R")
函式名稱 = function(引數) {
程式的內容........
}
函式名稱 = function(引數) {
print("Hello World, R")
}
再次提醒:愛護生命,千萬不要用中文當作變數及函式名字
helloworld <- function(){
print("Hello World, R")
}
helloworld()
## [1] "Hello World, R"
hi <- function(your_friend_name){
print(paste("Hi ", your_friends_name))
}
執行:
hi("Everybody")
## [1] "Hi, Everybody"
hi:函式名稱,
your_friend_name:此函式所帶入的參數,
my_friends=c("Dboy", "Ning", "Noah")
hi(my_friends)
## [1] "Hi, Dboy" "Hi, Ning" "Hi, Noah"
print("現在的時間和日期是:")
print(date())
## [1] "現在的時間和日期是:"
## [1] "Thu Aug 20 07:25:49 2015"
> date()
[1] "Thu Aug 20 03:50:13 2015"
print("現在的日期是:")
print(paste(substr(date(),21,24),
substr(date(),5,10), substr(date(),1,3)))
## [1] "現在的日期是:" ## [1] "2015 Aug 20 Thu"
print("現在的時間是:")
print(substr(date(),12,19))
## [1] "現在的時間是:" ## [1] "07:25:49"
now_date <- function(){
print("現在的日期是:")
print(paste(substr(date(),21,24),
substr(date(),5,10), substr(date(),1,3)))
}
now_time <- function(){
print("現在的時間是:")
print(substr(date(),12,19))
}
now <- function(){
now_date()
now_time()
}
now()
## [1] "現在的日期是:" ## [1] "2015 Aug 20 Thu" ## [1] "現在的時間是:" ## [1] "07:25:49"
save(list = ls(all = now_time), file= "now_time.RData")
save(list = ls(all = now_date), file= "now_date.RData")
save(list = ls(all = now), file= "now.RData")
save(list = ls(all = TRUE), file= "alldata.RData")
rm(list=ls())
load("alldata.RData")
numeric1 = c(2, 3, 5)
string1 = c("aa", "bb", "cc")
boolean1 = c(TRUE, FALSE, TRUE)
data1 = data.frame(numeric1, string1, boolean1)
## number1 string1 boolean1 ## 1 2 aa TRUE ## 2 3 bb FALSE ## 3 5 cc TRUE
dataf = data.frame(num1, str1, bol1)
func = function(str1) {
print(paste("Hello World, ", str1)) }
程式 =
資料 + function
by 尼克勞斯·維爾特, 1976
程式 =
資料 (V) + function
by 尼克勞斯·維爾特, 1976
程式 =
資料 (V) + function(V)
by 尼克勞斯·維爾特, 1976
?list example(factor)
善用「tab」按鍵
判斷條件控制
- if / else
迴圈控制
- for(),
除錯(找蟲)
- debug() / undebug()
程式錯誤及例外處理
- try() / tryCatch()
Syntex:
if (condition_1){
#Do something here....
} else if (conditon_2){
#Do something here
} else {
#Do something here
}
Note: **else if** and **else** are optional.
[TRUE] or [T]
[FALSE] or [F]
"1" == "1"
[1] TRUE
"1" == "2"
[1] FALSE
"1" <= "1"
[1] TRUE
"1" <= "2"
[1] FALSE
"1" < "2"
[1] TRUE
"1" < "1"
[1] FALSE
"1" >= "1"
[1] TRUE
"1" >= "2"
[1] FALSE
"2" > "1"
[1] TRUE
"1" > "1"
[1] FALSE
"2" != "1"
[1] TRUE
"1" != "1"
[1] FALSE
("1" == "1") && ("2" == "2")
[1] TRUE
("1" == "2") && ("2" == "2")
[1] FALSE
("1" == "1") || ("2" == "2")
[1] TRUE
("1" == "2") || ("2" == "2")
[1] TRUE
("1" == "2") || ("2" == "3")
[1] FALSE
『如果』 『年份』 『除以』 『400』
『餘數等於』 『0』
『就等於』『閏年』
如果((年份 對後數取餘數 400) 等於 0){
是不是閏年 = 是
}
if ((year %% 400) == 0){
is_leap_year = TRUE
}
if((year %% 4) == 0){
is_leap_year = TRUE
}
if((year %% 100) == 0){
is_leap_year = FALSE
}
if ((year1 %% 100)==0){
is_leap_year = FALSE
} else if ((year1 %% 4)==0){
is_leap_year = TRUE
} else {
is_leap_year = FALSE
}
if ((year1 %% 100) == 0){
is_leap_year = FALSE
} else if ((year1 %% 400) != 0){
is_leap_year = FALSE
}
if ((year1 %% 400)==0){
is_leap_year = TRUE
} else if ((year1 %% 100)==0){
is_leap_year = FALSE
} else if ((year1 %% 4)==0){
is_leap_year = TRUE
} else{
is_leap_year = FALSE
}
leap_year=function(year1){
if ((year1 %% 400)==0){
is_leap_year = TRUE
} else if ((year1 %% 100)==0){
is_leap_year = FALSE
} else if ((year1 %% 4)==0){
is_leap_year = TRUE
} else{
is_leap_year = FALSE
}
return(is_leap_year)
}
for (累進變數 in 開始條件:停止條件 )
1. 開始條件
2. 累進次數
3. 停止條件
for(x in 1:10) {
print(x)
}
## [1] 1 ## [1] 2 ## [1] 3 ## [1] 4 ## [1] 5 ## [1] 6 ## [1] 7 ## [1] 8 ## [1] 9 ## [1] 10
data_in = list(1, 2, 4, -5, "I^love^R", 0, 10)
for(x in data_in) {
print(x)
}
## [1] 1 ## [1] 2 ## [1] 4 ## [1] -5 ## [1] "I^love^R" ## [1] 0 ## [1] 10
for(年 in 開始年:結束年){
判斷是否是閏年
如果是閏年則次數 + 1
}
yearS = 1000
# yearS = as.integer(readline("input start year:"))
yearE = 2015
# yearE = as.integer(readline("input end year:"))
year_cnt = 0
for (year1 in yearS:yearE){
if ((year1 %% 400)==0){
is_leap_year = TRUE
} else if ((year1 %% 100)==0){
is_leap_year = FALSE
} else if ((year1 %% 4)==0){
is_leap_year = TRUE
} else{
is_leap_year = FALSE
}
if (is_leap_year == TRUE)
year_cnt = year_cnt + 1
}
paste("總共有:", year_cnt," 個閏年")
## [1] "總共有: 246 個閏年"
for (n in yearS:yearE){
if (leap_year(n) == TRUE){
year_cnt = year_cnt+1
}
}
paste("總共有:", year_cnt," 個閏年")
## [1] "總共有: 246 個閏年"
每天都在問神奇海螺
人都可能被劈腿,手一定有機會出鎚,
debug(fun)
undebug(fun)
debug(now_time)
now_time()
undebug(now_time)
猜數字的小遊戲(用if即可)
猜幾A幾B的遊戲
猜幾A幾B的遊戲:和電腦比賽 (還在寫)
圈叉遊戲 (用if, for 和matrix)
圈叉遊戲:只有平手或電腦必勝(還在寫)
河內塔(用for和recursive)
五子旗(還在寫)
撲克21點(還在寫)
data_in = list(1, 2, 4, -5, "I^love^R", 0, 10)
for(input in data_in) {
print(log(input))
}
[1] "0" [1] "0.693147180559945" [1] "1.38629436111989" [1] "NaN"
Error in log(input) : non-numeric argument to mathematical function 此外: Warning message: In log(input) : 產生了 NaNs
data_in = list(1, 2, 4, -5, "I^love^R", 0, 10)
for(input in data_in) {
try(print(log(input)))
}
## [1] 0 ## [1] 0.6931472 ## [1] 1.386294
## Warning in log(input): NaNs produced
## [1] NaN ## [1] -Inf ## [1] 2.302585
func_error = function(e){
print("this is a ERROR!!!")
}
func_warning = function(w){
print("just a wraning")
}
for(input in data_in) {
tryCatch(print(log(input)),
warning = func_warning ,
error = func_error)
}
## [1] 0 ## [1] 0.6931472 ## [1] 1.386294 ## [1] "just a wraning" ## [1] "this is a ERROR!!!" ## [1] -Inf ## [1] 2.302585
[1] 0 [1] 0.6931472 [1] 1.386294
[1] "just a wraning" [1] "this is a ERROR!!!" [1] -Inf [1] 2.302585
當然可以,but
https://www.calvin.edu/~scofield/courses/m143/materials/RcmdsFromClass.pdf
https://www.calvin.edu/~scofield/courses/m143/materials/RcmdsFromClass.pdf
num1 = c(2, 3, 5)
str1 = c("aa", "bb", "cc")
logic1 = c(TRUE, FALSE, TRUE)
df1 = data.frame(num1, str1, logic1)
# df1表示把data.frame()括號內的資料結合成為data.frame型態
## num1 str1 logic1 ## 1 2 aa TRUE ## 2 3 bb FALSE ## 3 5 cc TRUE
num1 = c(2, 3, 5)
str1 = c("aa", "bb", "cc")
logic1 = c(TRUE, FALSE, TRUE)
df2 = c(num1, str1, logic1)
# df2表示為c()括號中向量資料的結合
## [1] "2" "3" "5" "aa" "bb" "cc" "TRUE" "FALSE" "TRUE"
num1 = c(2, 3, 5)
str1 = c("aa", "bb", "cc")
logic1 = c(TRUE, FALSE, TRUE)
df3 = rbind(num1, str1, logic1)
# df3表示為rbind()括號中資料的結合
## [,1] [,2] [,3] ## num1 "2" "3" "5" ## str1 "aa" "bb" "cc" ## logic1 "TRUE" "FALSE" "TRUE"
source("iris.r")
iris_csv = read.csv("iris.csv")
View(iris_csv)
iris_tab = read.table("iris.txt")
View(iris_tab)
注意:這個需要額外的套件來支援excel格式,請先執行:library(gdata)
注意2:gdata套件的read.xls只能讀excel檔案中第一頁sheet,
library(gdata)
iris_xls = read.xls("iris.xls")
View(iris_xls)
ex1:
- A 說 B 在說謊,
- B 說 C 在說謊,
- C 說 A、B 都在說謊。
— 請問到底誰在說謊?